require 'extract/rules/rule'
require 'singleton'


module Extract
  
  class BrightkiteRules < Enumerable::Enumerator
    
    include Singleton
    
    public
    
    def collect(&block)
      @collections.each_pair {|k,v| yield v} if block
      @collections.values
    end
    
    #define de Brightkite extractions rules
    
    class Fullname < Rule
      
      def extract source_receiver, dest_receiver
        dest_receiver.full_name = source_receiver["fullname"].downcase unless source_receiver["fullname"].nil?
      end
      
    end
    
    class FirstLastName < Rule
      
      def extract source_receiver, dest_receiver
        if !source_receiver["fullname"].nil? then
          d = source_receiver["fullname"].match /(\w+) (\w+)/
          if !d.nil? then
            dest_receiver.first_name = d[1].downcase
            dest_receiver.last_name = d[2].downcase
          end
        end
      end
      
    end
    
    class Sex < Rule
      def extract source_receiver, dest_receiver
        if !source_receiver["sex"].nil? then
          dest_receiver.gender = source_receiver["sex"].eql?("male") ? "male" : "female"
        end
      end
    end
    
    class Age < Rule
      def extract source_receiver, dest_receiver
        if !source_receiver["age"].nil? then
          dest_receiver.age = source_receiver["age"].to_i == 0 ? nil : source_receiver["age"].to_i 
        end
      end
    end
    
    protected
    
    def initialize
      @collections = {}
      @collections[:Fullname] = Fullname.instance
      @collections[:Sex] = Sex.instance
      @collections[:Age] = Age.instance
      @collections[:FirstLastName] = FirstLastName.instance
    end
    
  end
  
end